/**
 * query.js
 * Version: 1.0
 */

var query = function () {
    var layer=getLayerByTitle('临时绘画图层');
    var drawPolygonBySelect=null;
    var tmpVectorLayer  = new ol.layer.Vector({
        title: '临时绘画图层',
        visible:true,
        source: new ol.source.Vector(),
        style: new ol.style.Style({
            fill: new ol.style.Fill({
                color: 'rgba(255,255,255,0)'
            }),
            stroke: new ol.style.Stroke({
                color: 'rgba(255,0,0,1)',
                width: 2
            }),
            image: new ol.style.Circle({
                radius:6,
                stroke: new ol.style.Stroke({
                    color:'rgba(0,243,255,1)'
                }),
                fill: new ol.style.Fill({
                    color: 'rgba(0,243,255,1)'
                })
            })
        })
    });
    /** 几何查询函数
     * @param {*} searchLayerName  查询图层名称
     * @param {*} geomtype 查询几何图形类别
     * @param {*}
     */
    this.SelectFeaturesByGeometry = function (geomtype,searchLayerName) {
        if(layer==null){
            YWLayer.getLayers().push(tmpVectorLayer);
        }
        var searchLayer = getLayerByTitle(searchLayerName);
        //创建一个交互式绘图对象
        drawPolygonBySelect = new ol.interaction.Draw({
            //绘制的数据源
            source: tmpVectorLayer.getSource(),
            //绘制类型
            type: geomtype,
            //样式
            style: new ol.style.Style({
                fill: new ol.style.Fill({
                    color:'rgba(255,255,0,0.3)'
                }),
                stroke: new ol.style.Stroke({
                    color: 'rgba(255,0,0,0.5)',
                    lineDash: [10, 10],
                    width:2
                }),
                image: new ol.style.Circle({
                    radius: 4,
                    stroke: new ol.style.Stroke({
                        color:'rgba(0,0,0,0.7)'
                    }),
                    fill: new ol.style.Fill({
                        color: 'rgba(255,255,255,0.2)'
                    })
                })
            })
        });
        //将交互绘图对象添加到地图中
        map.addInteraction(drawPolygonBySelect);
        //绘制结束事件
        drawPolygonBySelect.on('drawend', function (evt) {
            var feature=evt.feature;
            var geomType=feature.getGeometry().getType();
            console.info(geomType);
            if(geomType=='Polygon'){
                SelectPointByPolygon(searchLayer,feature);
                //bindliClick();
            }else if(geomType=='Circle'){
                SelectPointByCircle(searchLayer,feature);
               // bindliClick();
            }
        }, this);
    }

    function SelectPointByPolygon(searchLayer,feature) {
        var drawPolygon = turf.polygon(feature.getGeometry().getCoordinates());
        var pointFeatures=searchLayer.getSource().getFeaturesInExtent(feature.getGeometry().getExtent());
        for(var i=0;i<pointFeatures.length;i++){
            var turfpoint=turf.point(pointFeatures[i].getGeometry().getCoordinates());
            if(turf.booleanContains(drawPolygon,turfpoint)){
                tmpVectorLayer.getSource().addFeature(pointFeatures[i]);
            }
        }
    }
    function SelectPointByCircle(searchLayer,feature) {
        tmpVectorLayer.getSource().clear();
        var pointFeatures=searchLayer.getSource().getFeaturesInExtent(feature.getGeometry().getExtent());
        for(var i=0;i<pointFeatures.length;i++){
            if(feature.getGeometry().intersectsCoordinate(pointFeatures[i].getGeometry().getCoordinates())){
                tmpVectorLayer.getSource().addFeature(pointFeatures[i]);
            }
        }
    }
    
    this.undoSearch = function () {
        //删除临时图层
        YWLayer.getLayers().remove(getLayerByTitle('临时绘画图层'));
    };
    /** 属性查询函数
     * @param {*} searchLayerName  查询图层
     * @param {*} property_name 查询的属性名称
     * @param {*}property_value 查询的属性值
     */
    var selectedByAttriFeature ;
    this.selectByAttribute = function (searchLayerName,property_name,property_value) {
        var styles =  new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 20,
                    fill: new ol.style.Fill({
                        color: '#1f26ff'
                    }),
                    stroke: new ol.style.Stroke({
                        color: '#4eff2d',
                        width: 2
                    })
                })
            })



        var searchLayer = getLayerByTitle(searchLayerName);
        var features = searchLayer.getSource().getFeatures();
        for(var i=0;i<features.length;i++){
            if (features[i].get(property_name) === property_value) {
                selectedByAttriFeature = features[i];
                selectedByAttriFeature.setStyle(styles);
                tmpVectorLayer.getSource().addFeature(selectedByAttriFeature);
                var my_view = new ol.View({
                    center: getCenterOfExtent(selectedByAttriFeature.getGeometry().getExtent()),
                    zoom: 14
                });
                map.setView(my_view);
            }
        }

    }
    function getCenterOfExtent(Extent){
        var X = Extent[0] + (Extent[2]-Extent[0])/2;
        var Y = Extent[1] + (Extent[3]-Extent[1])/2;
        return [X, Y];
    }
};